/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ru.rantipin.GUI;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.BorderFactory;
import javax.swing.JComboBox;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.table.DefaultTableModel;
import ru.rantipin.persistence.PersistenceHelper;
//import ru.rantipin.persistence.Calander;
//import ru.rantipin.persistence.Formula;
//import ru.rantipin.persistence.Component;
//import ru.rantipin.persistence.Element;
//import ru.rantipin.persistence.Research;
//import ru.rantipin.persistence.Parameter;
import java.util.ArrayList;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.JTextArea;

/**
 *
 * @author rantipin
 */
public class DataBaseElementPanel extends JPanel{
    
    public static class ElementPanelParams {
        
        public ElementPanelParams(String titleTable, String ... column) {
            this.titleTable = titleTable;
            this.column = column;
        }
        
        public String getTitleTable() {
            return titleTable;
        }
                
        public String[] getColumn() {
            return column;
        }
                
        private String titleTable = null;
        private String[] column = null;
    }
    
    
    public DataBaseElementPanel(ElementPanelParams param) {
        this.param = param;
        if (param.getTitleTable() == "Calender elements") {
            flag = true;
        }
        
        GridBagLayout gridbag = new GridBagLayout();
        setLayout(gridbag);
        
        gridbag.setConstraints(getPreserveValues(), 
                new GridBagConstraints(
                    0, 0,
                    GridBagConstraints.REMAINDER, 1,
                    0.0, 0.0,
                    GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL,
                    new Insets(0, 10, 0, 10),
                    0, 0
                ));
        add(getPreserveValues());
        
        gridbag.setConstraints(getDescriptionLabel(), 
                new GridBagConstraints(
                    0, 4,
                    1, 1,
                    0.0, 0.0,
                    GridBagConstraints.LINE_START, GridBagConstraints.NONE,
                    new Insets(10, 10, 0, 10),
                    0, 0
                ));
        add(getDescriptionLabel());
        
        JScrollPane area = new JScrollPane(getTextArea());
        area.setMinimumSize(new Dimension(0, 60));
        area.setPreferredSize(new Dimension(0, 60));
        gridbag.setConstraints(area, 
                new GridBagConstraints(
                    0, 5,
                    GridBagConstraints.REMAINDER, 1,
                    1.0, 1.0,
                    GridBagConstraints.LINE_START, GridBagConstraints.BOTH,
                    new Insets(10, 10, 0, 10),
                    0, 0
                ));
        add(area);
        
        JPanel pane = new JPanel();
        pane.setMinimumSize(new Dimension(0, 100));
        pane.setPreferredSize(new Dimension(0, 100));
        pane.setBorder(BorderFactory.createTitledBorder(param.getTitleTable()));
        pane.setLayout(new BorderLayout());
        pane.add(new JScrollPane(getTable()));
        
        gridbag.setConstraints(pane, 
                new GridBagConstraints(
                    0, 6,
                    GridBagConstraints.REMAINDER, 1,
                    0.0, 0.0,
                    GridBagConstraints.LINE_START, GridBagConstraints.BOTH,
                    new Insets(10, 10, 0, 10),
                    0, 0
                ));
        add(pane);
    }
    
    private JComboBox getPreserveValues() {
        if (preserveValues == null) {
            preserveValues = new JComboBox();
            preserveValues.setEditable(false);
            preserveValues.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    JComboBox combobox = (JComboBox)e.getSource();
                    Object selectedItem = combobox.getSelectedItem();
                    if (selectedItem != null) { 
//                        if(flag) {
//                        Calander calander = PersistenceHelper.getInstance().getCalanderData(selectedItem);
//                        discriptionArea.setText(calander.getDescription());
//                        Collection<Element> elements = calander.getElementCollection();
//                        for (Element it: elements){
//                            tbmodel.addRow(new Object[]{it.getName(), it.getUnits(), "", "", it.getDescription()});
//                        }
//                        } else {
//                            Formula formula = PersistenceHelper.getInstance().getFormulaData(selectedItem);
//                            discriptionArea.setText(formula.getDescription());
//                            setFormulaTableValues(formula.getComponentCollection());
//                        }
                    }
                    }
            });
        }
        return preserveValues;
    }
    
    public void setPreserveValues() {
//        ArrayList temp;
//        if (flag){
//            temp = PersistenceHelper.getInstance().getCalanderNames();
//        } else {
//            temp = PersistenceHelper.getInstance().getFormulaNames();
//        }
//        for (int i = 0; i < temp.size(); i++) {
//            preserveValues.addItem(temp.get(i));
//        }
    }
    
//    private void setFormulaTableValues (Collection<Component> components) {
//        tbmodel.setRowCount(0);
//        for(Component it: components) {
//            tbmodel.addRow(new Object[]{it.getName(), it.getMass(),
//            it.getPercent(), it.getDescription()});
//        }
//    }
    
//    private void setCalanderTableValues(Collection<Parameter> parameters) {
//        tbmodel.setRowCount(0);
//        for (Parameter it: parameters) {
//           tbmodel.addRow(new Object[]{it.getName(), it.getUnits(), it.getValue(),
//           it.getDeviation(), it.getDescription()});
//        }
//    }
    
//    public void setFormulaPanelValues(Research research) {
//        Formula formula = research.getIdFormula();
//        preserveValues.removeAllItems();
//        preserveValues.addItem(new Item(formula.getId(), formula.getName()));
//        discriptionArea.setText(formula.getDescription());
//        setFormulaTableValues(formula.getComponentCollection());
//    }
    
//    public void setCalanderPanelValues(Research research) {
//        Calander calander = research.getIdCalander();
//        preserveValues.removeAllItems();
//        preserveValues.addItem(new Item (calander.getId(), calander.getName()));
//        discriptionArea.setText(calander.getDescription());
//        setCalanderTableValues(research.getParameterCollection());
//    }

    public void clearPanel(){
        preserveValues.removeAllItems();
        discriptionArea.setText("");
        discriptionArea.setEditable(true);
        tbmodel.setRowCount(0);
    }
    
    public Object getComboboxObject(){
         return preserveValues.getSelectedItem();
    }
    
    private JLabel getDescriptionLabel() {
        if (descriptionLabel == null) {
                descriptionLabel = new JLabel("Description:");
        }
        
        return descriptionLabel;
    }

    private JTextArea getTextArea() {
        if (discriptionArea == null) {
                discriptionArea = new JTextArea();
                discriptionArea.setLineWrap(true);
                discriptionArea.setWrapStyleWord(true);
                discriptionArea.setEditable(false);
        }
        
        return discriptionArea;
    }
    
     class UserTableModel extends DefaultTableModel {
        @Override
        public boolean isCellEditable(int rowIndex, int columnIndex) {
                if (getColumnCount() == 5 && (columnIndex == 2 || columnIndex == 3)) {
                    return flag;
                } else {
                    return false;
                }
        }
    } 
     
//    public Collection<Parameter> getParametersFromTable() {
//        List<Parameter> parameters = new ArrayList();
//        for (int i = 0; i < tbmodel.getRowCount(); i++) {
//            Parameter tmpParameter = new Parameter();
//            String tmp = tbmodel.getValueAt(i, 0).toString();
//            if ( tmp != "" || tmp != null ) {
//                tmpParameter.setName(tmp);
//                tmp = tbmodel.getValueAt(i, 1).toString();
//                 if ( tmp != "" || tmp != null ) {
//                    tmpParameter.setUnits(tmp);
//                    String dataValue = tbmodel.getValueAt(i, 2).toString();
//                    String dataDeviation = tbmodel.getValueAt(i, 3).toString();
//                    try{
//                        double number1 = Double.valueOf(dataValue.trim()).doubleValue();
//                        double number2 = Double.valueOf(dataDeviation.trim()).doubleValue();
//                        tmpParameter.setValue(number1);
//                        tmpParameter.setDeviation(number2);
//                    } catch (NumberFormatException nfe) {
//                        JOptionPane.showMessageDialog(DataBaseElementPanel.this, "Data in columns value and deviation must be numbers",
//                                  "Data Error", JOptionPane.ERROR_MESSAGE);
//                    }
//                    tmp = tbmodel.getValueAt(i, 4).toString();
//                    if ( tmp != "" || tmp != null ) {
//                        tmpParameter.setDescription(tmp);
//                    } else {
//                        continue;
//                    }
//                 } else {
//                    continue;
//                 }
//            } else {
//                continue;
//            }
//            parameters.add(tmpParameter);
//        }
//        return parameters;
//    }
    
    private JTable getTable() {
        if (table == null) {
            tbmodel = new UserTableModel();
            tbmodel.setColumnIdentifiers(param.getColumn());
            table = new JTable(tbmodel);
            table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
//            tbmodel.addTableModelListener( new TableModelListener() {
//                public void tableChanged (TableModelEvent e) {
//                    int rowIndex = e.getFirstRow();
//                    int columnIndex = e.getColumn();
//                    TableModel model = (TableModel)e.getSource();
//                    String columnName = model.getColumnName(columnIndex);
//                    String data = model.getValueAt(rowIndex, columnIndex).toString();
//                    try{
//                        double number = Double.valueOf(data.trim()).doubleValue();
//                        PersistenceHelper.getInstance()
//                            .setDataParameter(columnName, number);
//                    } catch (NumberFormatException nfe) {
//                        JOptionPane.showMessageDialog(DataBaseElementPanel.this, "Data in column" + columnName + "must be number", 
//                                      "Data Error", JOptionPane.ERROR_MESSAGE);
//                    }
//                }
//            });  
        }
        
        return table;
    }
    
    private ElementPanelParams param = null;
    private JComboBox preserveValues = null;
    private JLabel descriptionLabel = null;
    private JTextArea discriptionArea = null;
    private JTable table = null;
    private UserTableModel tbmodel = null;
    private boolean flag = false;
}
